home *** CD-ROM | disk | FTP | other *** search
- Path: crc-news.doc.ca!usenet
- From: Slobodan Celenkovic <Celenkovic.Bob@ic.gc.ca>
- Newsgroups: comp.lang.c++
- Subject: Re: Errors in constructors
- Date: 26 Mar 1996 22:44:12 GMT
- Organization: Industry Canada
- Message-ID: <4j9rvs$bi7@crc-news.doc.ca>
- References: <4is49h$k1o@usenetp1.news.prodigy.com>
- NNTP-Posting-Host: bouhadrah.mohamed.bsd001.ic.gc.ca
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.22 (Windows; U; 16bit)
-
- Here is what I do:
- I declare an RC (return code) variable in the module. Every method
- including constartor & destructor sets the RC var indicating success
- (errOk), or failure (errXxxx). Then simply check the RC:
-
- // Module MyObject
-
- int moRC ; // return code
-
- MyObject::MyObject() // default constructor
- {
- moRC = errOk ; // init to Ok
- // initializaton code
- SomeTable = (char*) malloc( SomeSize );
- if ( SomeTable == NULL ) {
- moRC = errNotEnoughMemory ;
- return;
- }
- // more initialization
- SomeFile = fopen("SomeName","r");
- if (SomeFile == NULL) {
- moRC = errno ;
- return;
- }
- }
-
- ----------
-
- main()
- { MyObject a ;
-
- if ( moRC != errOk ) { constructor failed }
-